﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Controls;

namespace WasaWasa.Converters
{
    public class DecimalRangeValidationRule : ValidationRule
    {
        decimal minValue = decimal.MinValue;

        public decimal MinValue
        {
            get { return minValue; }
            set
            {
                if (value > MaxValue)
                    throw new ArgumentException();
                minValue = value;
            }
        }

        decimal maxValue = decimal.MaxValue;

        public decimal MaxValue
        {
            get { return maxValue; }
            set
            {
                if (value < MinValue)
                    throw new ArgumentException();
                maxValue = value;
            }
        }

        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
        {
            decimal d;
            bool b = decimal.TryParse(value as string, out d);
            return new ValidationResult(b && minValue <= d && d <= maxValue, null);
        }
    }
}
